﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Forum
{
    public class FourmReply:IForumReply
    {
        public void ReplyToForum(int forumId, int userId, string replyText, ref Model.NutriLeafDBEntities context)
        {
         
            Model.ForumThreadReply re = new Model.ForumThreadReply();
          
            re.UserID = userId;
            re.ReplyText = replyText;
            re.DateTimeStamp = DateTime.Now;
            re.ForumThreadReplyID=forumId;
            context.ForumThreadReplies.AddObject(re);
            context.SaveChanges();
        }

        public void MarkAsAnswer(int forumReplyId, ref Model.NutriLeafDBEntities context)
        {
            context.ForumThreadReplies.Where(p => p.ForumThreadReplyID == forumReplyId).First().IsAnswer = true;
            context.SaveChanges();
        }

        public List<Model.ForumThreadReply> GetAllReplies(int forumId, ref Model.NutriLeafDBEntities context)
        {
            return context.ForumThreads.Where(p => p.ForumThreadID == forumId).First().ForumThreadReplies.ToList();
        }

        public Model.ForumThreadReply GetReplyById(int forumId, ref Model.NutriLeafDBEntities context)
        {
          return  context.ForumThreadReplies.Where(p => p.ForumThreadReplyID == forumId).First();
        }

        public bool IsAnswer(int forumReplyId, ref Model.NutriLeafDBEntities context)
        {
            return context.ForumThreadReplies.Where(p => p.ForumThreadReplyID == forumReplyId).First().IsAnswer.Value;
        }

        public void RemoveRpely(int forumReplyId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.ForumThreadReplies.Where(p => p.ForumThreadReplyID == forumReplyId).First());
            context.SaveChanges();
        }
    }
}